//
//  AddBlankViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/11.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "AddBlankViewController.h"
#import "CheckBalnkViewController.h"

@interface AddBlankViewController ()<UITextFieldDelegate>

@property(nonatomic ,strong) UITextField *passWord;
@property(nonatomic ,strong) UITextField *passWord1;
@property(nonatomic ,strong) UIButton *MySureBtn;
@property(nonatomic ,assign) CGFloat height;

@end

@implementation AddBlankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)createView{
    self.title=@"绑定支付宝";
    self.height=30;
    UIView *bottom=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 50)];
    bottom.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bottom];
    MYLabel*UserName=[[MYLabel alloc]init];
    UserName.text=MyString(@"银行卡号");
    UserName.textColor=TextColor6;
    [bottom addSubview:UserName];
    CGSize size=[UserName.text sizeWithAttributes:@{NSFontAttributeName:FontAndStyle14}];
    UIView*v=[[UIView alloc]init];
    v.backgroundColor=Gray_Color;
    [bottom addSubview:v];
    
    
    self.passWord=[[UITextField alloc]initWithFrame:CGRectMake(10, 30, MainW-20, 49)];
    self.passWord.placeholder=MyString(@"卡号");
    [self.passWord setValue:[UIFont boldSystemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
    //self.passWord.delegate=self;
    [self.passWord addTarget:self action:@selector(controlTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.passWord.font=FontAndStyle14;
    self.passWord.textColor=TextColor3;
    self.passWord.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord.borderStyle=0;
    [bottom addSubview:self.passWord];
    
    
    self.passWord1=[[UITextField alloc]initWithFrame:CGRectMake(10, 110, MainW-20, 49)];
    self.passWord1.font=FontAndStyle14;
    self.passWord1.textColor=TextColor3;
    self.passWord1.hidden=YES;
    self.passWord1.placeholder=MyString(@"招商银行储蓄卡");
    [self.passWord1 setValue:[UIFont boldSystemFontOfSize:11]forKeyPath:@"_placeholderLabel.font"];
    //self.passWord1.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord1.borderStyle=0;
    [self.view addSubview:self.passWord1];
    UIImageView *headImg=[[UIImageView alloc]initWithFrame:CGRectMake(5, -1, 15, 15)];
    headImg.image=[UIImage imageNamed:@"icon_return"];
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, 15)];
    view.backgroundColor=[UIColor clearColor];
    [view addSubview:headImg];
    self.passWord1.leftView = view;
    self.passWord1.leftViewMode = UITextFieldViewModeAlways;
    
    
   self.MySureBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 220, MainW-20, 50)];
    [self.MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.MySureBtn setTitle:MyString(@"下一步") forState:UIControlStateNormal];
    [self.MySureBtn addTarget:self action:@selector(NextSureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.MySureBtn setBackgroundColor:Red_Color];
    [self.view addSubview:self.MySureBtn];
    ViewRadius(self.MySureBtn, 5);
    
    
    [UserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo((50-size.height)/2);
        make.left.equalTo(10);
        make.width.mas_equalTo(size.width);
        make.height.mas_equalTo(size.height);
    }];
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.left.equalTo(UserName.mas_right).offset(25);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passWord.mas_bottom);
        make.left.equalTo(0);
        make.width.mas_equalTo(MainW);
        make.height.mas_equalTo(1);
    }];
    [self.passWord1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v.mas_bottom).offset(12);
        make.left.equalTo(10);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(15);
    }];
    [self.MySureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passWord.mas_bottom).offset(self.height);
        make.right.mas_equalTo(-10);
        make.left.mas_equalTo(10);
        make.height.mas_equalTo(50);
    }];

}
-(void)controlTextDidChange:(NSNotification *)obj {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(nizana) withObject:obj afterDelay:0.1];
}
-(void)nizana{
    if (self.passWord.text.length==5) {
        self.passWord1.hidden=NO;
        self.height=58;
        self.MySureBtn.frame=CGRectMake(10, 108, MainW-20, 50);
    }else{
        self.MySureBtn.frame=CGRectMake(10, 80, MainW-20, 50);
        self.passWord1.hidden=YES;
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self.passWord1 resignFirstResponder];
}

-(void)NextSureClick{
    CheckBalnkViewController*vc=[[CheckBalnkViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //判断是否是当前输入框
    if (self.passWord == textField) {
        NSString *text = self.passWord.text;
        //限制字符 至于那个\b用在search中 写不写都行
        NSCharacterSet* characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        //去掉空格
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        //检查除数字之外的字符 invertedSet:意思是取反,除了数字和退格的内容
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
        //拼接text  要输入的部分+原有的
        text = [text stringByReplacingCharactersInRange:range withString:string];
        //去空格
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString* newString = @"";
        //四位添加空格
        while (text.length > 0) {
            NSString* subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        //去掉除数字部分
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        // 限制长度
        if (newString.length >= 24) {
            return NO;
        }
        //赋值
        [self.passWord setText:newString];
        return NO;
    }
    return YES;
}

@end
